/**
 * 每个加油站有gas[i]升油，每段路要消耗cost[i]升油，初始油箱为空，求可以绕路一周的起点加油站编号
 * @param {number[]} gas 
 * @param {number[]} cost 
 */
var canCompleteCircuit = function (gas, cost) {
    let totalGas = 0
    let totalCost = 0
    for (let i = 0; i < gas.length; i++) {
        totalGas += gas[i]
        totalCost += cost[i]
    }

    if (totalGas < totalCost) {
        // 总油量比总消耗量小就不可能走完
        return -1
    }

    // 记录每到一个点还有的油量
    let currentGas = 0
    // 记录加油站编号
    let start = 0
    for (let i = 0; i < gas.length; i++) {
        currentGas = currentGas + gas[i] - cost[i]
        if (currentGas < 0) {
            // 某点油量小于0说明走不通，直接跳到下一个点重新开始
            currentGas = 0
            start = i + 1
        }
    }

    return start
}